package easier.recycler;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import java.util.List;

/**
 * {@link MultiTypeAdapter#updateViewHolders(List)}刷新界面
 */
public class TypeViewHolderDiffCallBack extends DiffUtil.Callback {

    private final List< TypeViewHolder > mOldList;
    private final List< TypeViewHolder > mNewList;

    public TypeViewHolderDiffCallBack( List< TypeViewHolder > oldList,
        List< TypeViewHolder > newList ) {

        mOldList = oldList;
        mNewList = newList;
    }

    @Override
    public int getOldListSize() {

        return mOldList == null ? 0 : mOldList.size();
    }

    @Override
    public int getNewListSize() {

        return mNewList == null ? 0 : mNewList.size();
    }

    @Override
    public boolean areItemsTheSame( int oldItemPosition, int newItemPosition ) {

        TypeViewHolder a = mOldList.get( oldItemPosition );
        TypeViewHolder b = mNewList.get( newItemPosition );

        if ( a != null && b != null ) {
            return a.itemEquals( b );
        } else {
            return false;
        }
    }

    @Override
    public boolean areContentsTheSame( int oldItemPosition, int newItemPosition ) {

        TypeViewHolder a = mOldList.get( oldItemPosition );
        TypeViewHolder b = mNewList.get( newItemPosition );

        if ( a != null && b != null ) {
            return a.contentEquals( b );
        } else {
            return false;
        }
    }

    /**
     * 实现该方法主要为提升效率,返回的不是null,那么将不会新创建ViewHolder,而是使用现在位置的ViewHolder重新绑定界面
     *
     * @return 不为null, 刷新自己
     */
    @Nullable
    @Override
    public Object getChangePayload( int oldItemPosition, int newItemPosition ) {

        return "";
    }
}
